/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.ipti.br.tag.bean.dataManipulation;

import org.ipti.br.tag.entity.UnitySchedule;
import org.ipti.br.tag.entity.enums.WeekDay;
import java.io.Serializable;
import java.util.Calendar;
import java.util.Date;
import org.primefaces.model.ScheduleEvent;

/**
 *
 * @author luancorumba
 */
public class UnityScheduleEvent implements ScheduleEvent,Serializable{

    private UnitySchedule unitySchedule;

    public UnityScheduleEvent(){
        unitySchedule = new UnitySchedule();
    }
    
    public UnityScheduleEvent(UnitySchedule unitySchedule){
        this.unitySchedule = unitySchedule;
    }
    
    public UnityScheduleEvent(Date date){
        Calendar cI = Calendar.getInstance(),cF = Calendar.getInstance();
        cI.setTime(date);
        cF.setTime(date);
        unitySchedule = new UnitySchedule();
        unitySchedule.setWeekDay(WeekDay.getDay(cI.get(Calendar.DAY_OF_WEEK)));
        unitySchedule.setInitialTime(cI);
        unitySchedule.setFinalTime(cF);
    }
    
    public void updateWeekDay(){                                                //YES! this method is necessary
        int wd = unitySchedule.getWeekDay().ordinal();
        
        Calendar c = Calendar.getInstance();
        c.set(Calendar.DAY_OF_MONTH,c.get(Calendar.DAY_OF_MONTH) - (c.get(Calendar.DAY_OF_WEEK) - 1));  //setToSunday
        c.set(Calendar.DAY_OF_MONTH,c.get(Calendar.DAY_OF_MONTH) + wd);                                 //setToRealDay
        c.set(Calendar.HOUR_OF_DAY, unitySchedule.getInitialTime().get(Calendar.HOUR_OF_DAY));
        c.set(Calendar.MINUTE, unitySchedule.getInitialTime().get(Calendar.MINUTE));
        unitySchedule.setInitialTime(c);
        
        c = Calendar.getInstance();
        c.set(Calendar.DAY_OF_MONTH,c.get(Calendar.DAY_OF_MONTH) - (c.get(Calendar.DAY_OF_WEEK) - 1));  //setToSunday
        c.set(Calendar.DAY_OF_MONTH,c.get(Calendar.DAY_OF_MONTH) + wd);                                 //setToRealDay
        c.set(Calendar.HOUR_OF_DAY, unitySchedule.getFinalTime().get(Calendar.HOUR_OF_DAY));
        c.set(Calendar.MINUTE, unitySchedule.getFinalTime().get(Calendar.MINUTE));
        unitySchedule.setFinalTime(c);
    }
    
    @Override
    public String getId() {
        return (unitySchedule != null && unitySchedule.getId() != null) ? unitySchedule.getId().toString() : "";
    }

    @Override
    public void setId(String id) {
    }

    @Override
    public Object getData() {
        return unitySchedule;
    }

    @Override
    public String getTitle() {
        return (unitySchedule != null
                && unitySchedule.getDiscipline() != null
                && unitySchedule.getTask() != null) 
                    ? unitySchedule.getTask().getName() + " " +
                    unitySchedule.getDiscipline().getName(): null;
    }

    @Override
    public Date getStartDate() {
        return (unitySchedule != null) ? unitySchedule.getInitialTime().getTime() : null;
    }
    
    public void setStartDate(Date startDate){
        this.unitySchedule.getInitialTime().setTime(startDate);
    }

    @Override
    public Date getEndDate() {
        return (unitySchedule != null) ? unitySchedule.getFinalTime().getTime() : null;
    }
    
    public void setEndDate(Date endDate){
        this.unitySchedule.getFinalTime().setTime(endDate);
    }

    @Override
    public boolean isAllDay() {
        return false;
    }

    @Override
    public String getStyleClass() {
        return "";
    }

    @Override
    public boolean isEditable() {
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 97 * hash + (this.unitySchedule != null ? this.unitySchedule.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final UnityScheduleEvent other = (UnityScheduleEvent) obj;
        if (this.unitySchedule != other.unitySchedule &&
                (this.unitySchedule == null || !this.unitySchedule.equals(other.unitySchedule))) {
            return false;
        }
        return true;
    }
}
